﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;


namespace RunVistaGadget31
{
    /// <summary>
    /// 
    /// </summary>
    public class GadgetProperties
    {
        /// <summary>
        /// 
        /// </summary>
        public static String DefaultRegKey = "0";

        private const String ADDIN_REGISTRYKEY = @"SOFTWARE\Microsoft\VisualStudio\10.0";

        /// <summary>
        /// 
        /// </summary>
        public String GadgetOutputPath
        {
            get
            {
                return GetRegistryKey("GadgetOutputPath");
            }
            set
            {
                SetRegistryKey(value, "GadgetOutputPath");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String ArchivatorPath
        {
            get
            {
                return GetRegistryKey("ArchivatorPath");
            }
            set
            {
                SetRegistryKey(value, "ArchivatorPath");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String SigntoolPath
        {
            get
            {
                return GetRegistryKey("SigntoolPath");
            }
            set
            {
                SetRegistryKey(value, "SigntoolPath");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String PfxFilePath
        {
            get
            {
                return GetRegistryKey("PfxFilePath");
            }
            set
            {
                SetRegistryKey(value, "PfxFilePath");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String PfxPassword
        {
            get
            {
                return GetRegistryKey("PfxPassword");
            }
            set
            {
                SetRegistryKey(value, "PfxPassword");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String AllowedExtensionsForGadget
        {
            get
            {
                String _AllowedExtensionsForGadget = GetRegistryKey("AllowedExtensionsForGadget");
                if (String.IsNullOrEmpty(_AllowedExtensionsForGadget))
                {
                    _AllowedExtensionsForGadget = "*.html;*.js;*.vbs;*.xml;*.jpg;*.png;*.css;*.gif";
                }

                return _AllowedExtensionsForGadget;
            }
            set
            {
                SetRegistryKey(value, "AllowedExtensionsForGadget");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String ShortcutForRunGadget
        {
            get
            {
                String RegKeyValue = GetRegistryKey("ShortcutForRunGadget");
                if (String.IsNullOrEmpty(RegKeyValue))
                {
                    RegKeyValue = "ctrl+r, ctrl+r";
                }
                return RegKeyValue;
            }
            set
            {
                SetRegistryKey(value, "ShortcutForRunGadget");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsBuildSignedGadget
        {
            get
            {
                RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(ADDIN_REGISTRYKEY, false);

                string registryValue = (string)registryKey.GetValue
                    ("IsBuildSignedGadget", "0");

                if (registryValue == "0")
                    registryValue = string.Empty;

                return !String.IsNullOrEmpty(registryValue);
            }
            set
            {
                string registryValue;

                RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(ADDIN_REGISTRYKEY, true);
                if (value == true)
                    registryValue = "1";
                else
                    registryValue = "0";

                registryKey.SetValue("IsBuildSignedGadget", registryValue, RegistryValueKind.String);
            }
        }

        private String GetRegistryKey(String regKey)
        {
            RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(ADDIN_REGISTRYKEY, false);

            string registryValue = (string)registryKey.GetValue(regKey, "0");

            if (registryValue == "0")
                return string.Empty;

            return registryValue;
        }

        private void SetRegistryKey(String value, String regKey)
        {
            string registryValue;

            RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(ADDIN_REGISTRYKEY, true);
            if (!string.IsNullOrEmpty(value))
                registryValue = value;
            else
                registryValue = "0";

            registryKey.SetValue(regKey, registryValue, RegistryValueKind.String);
        }

    }
}
